package com.example.bot_cc.socket;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView hostIP;
    private EditText serverIP, serverPort, content;
    private Button getHostIP, connect, disconnect, send;
    private Handler handler = new Handler() {
        public void handleMessage(Message message){
            String hostip = (String)message.obj;
            hostIP.setText(hostip);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }



    private void init(){
        hostIP = (TextView)findViewById(R.id.host_IP);
        serverIP = (EditText)findViewById(R.id.server_ip);
        serverPort = (EditText)findViewById(R.id.server_port);
        content = (EditText)findViewById(R.id.content);
        getHostIP = (Button)findViewById(R.id.get_host_ip);
        connect = (Button)findViewById(R.id.connect);
        disconnect = (Button)findViewById(R.id.disconnect);
        send = (Button)findViewById(R.id.send);

        getHostIP.setOnClickListener(this);
        connect.setOnClickListener(this);
        disconnect.setOnClickListener(this);
        send.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.get_host_ip:
                new MySocket(handler).start();
                break;
            case R.id.connect:{
                new MyConnect(serverIP.getText().toString(), serverPort.getText().toString(),content.getText().toString()).start();
                break;
            }
            case R.id.disconnect:
                break;
            case R.id.send:
                break;
        }
    }
}
